/*
 * ScrollPaneDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import com.cute4j.swing.Button;
import com.cute4j.swing.Component;
import com.cute4j.swing.Container;
import com.cute4j.swing.ProgressBar;
import com.cute4j.swing.TextArea;
import com.cute4j.swing.Timer;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;
import com.cute4j.swing.layouts.BorderLayout;
import com.cute4j.swing.layouts.formlayout.CellConstraints;
import com.cute4j.swing.layouts.formlayout.FormLayout;

/**
 * Demonstration of the ProgressBar class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class ProgressBarDemo extends DemoModule {

// Private Fields. -------------------------------------------------------------

  private Timer timer;

// Public Constructor. ---------------------------------------------------------

  /**
   * Constructs new ProgressBarDemo object.
   */
  public ProgressBarDemo() {
    super("ProgressBar Demo", "Demonstration of the ProgressBar class.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    final String text = getText("ProgressBarDemo.text");
    Container panel = new Container(new BorderLayout());

    final TextArea textArea = new TextArea();
    textArea.setEditable(false);
    panel.add(textArea, BorderLayout.CENTER);

    FormLayout layout = new FormLayout(
                "p:g, max(180dlu;p), 4dlu, p, 4dlu, p, p:g", "5dlu, p, 4dlu");
    Container progressPanel = new Container(layout);
    CellConstraints cc = new CellConstraints();
    final ProgressBar progressBar = new ProgressBar();
    progressBar.setMaximum(text.length());
    progressPanel.add(progressBar, cc.xy(2, 2));
    final Button startButton = new Button("Start Loading Text");
    final Button stopButton = new Button("Stop Loading Text");
    startButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        startButton.setEnabled(false);
        if (progressBar.getValue() == text.length()) {
          textArea.clearText();
          progressBar.setValue(0);
        }
        timer = new Timer(18, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            int index = progressBar.getValue();
            if (index < text.length()) {
              textArea.appendText(text.substring(index, ++index));
              progressBar.setValue(index);
            } else {
              stopButton.setEnabled(false);
              timer.stop();
              timer = null;
              startButton.setEnabled(true);
            }
          }
        });
        timer.start();
        stopButton.setEnabled(true);
      }
    });
    stopButton.setEnabled(false);
    stopButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        stopButton.setEnabled(false);
        timer.stop();
        timer = null;
        startButton.setEnabled(true);
      }
    });
    progressPanel.add(startButton, cc.xy(4, 2));
    progressPanel.add(stopButton, cc.xy(6, 2));
    panel.add(progressPanel, BorderLayout.SOUTH);

    return panel;
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new ProgressBarDemo().show();
  }

}
